package iotwisp.base.util;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import iotwisp.base.cqrs.BusinessException;
import org.hibernate.validator.HibernateValidator;

public class ValidationUtils {

	/**
	 * 使用hibernate的注解来进行验证
	 * 
	 */
	private static Validator validator = Validation.byProvider(HibernateValidator.class).configure().failFast(true)
			.buildValidatorFactory().getValidator();

	/**
	 * 功能描述: <br>
	 * 〈注解验证参数〉
	 *
	 * @param obj
	 * @throws BusinessException
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	public static <T> void validate(T obj) throws BusinessException {
		Set<ConstraintViolation<T>> constraintViolations = validator.validate(obj);
		// 抛出检验异常
		if (constraintViolations.size() > 0) {
			throw BusinessException.getInstance(BusinessException.PARAM_ILLEGALITY,
					String.format("参数校验失败:%s", constraintViolations.iterator().next().getMessage()));
		}
	}
}
